package com.long1008.controller.personnel;

import com.long1008.pojo.*;
import com.long1008.pojo.*;
import com.long1008.service.EmpService;
import com.long1008.service.PersonnelService;
import com.long1008.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/personnel/adjust")
public class EmpSalaryController {

    @Autowired
    PersonnelService personnelService;
    @Resource
    EmpService empService;
    @Resource
    SalaryService salaryService;
    /**
     * 根据页码和大小分页查询
     * @param page 当前页，默认为1
     * @param size 当前每页显示行数，默认为10
     * @return 页信息的实体
     */
    @GetMapping("/salary/getall")
    public RespPageEntity getAdjustSalarysByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return personnelService.getAdjustSalarysByPage(page, size);
    }

    //根据员工姓名筛选调薪记录
    @RequestMapping(value = "/salary/keywords/{keywords}")
    public RespPageEntity getAdjustSalarysByKeywords(
            @PathVariable(required = false) String keywords,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size ) {
        return personnelService.getAdjustSalarysByKeywords(keywords, page, size);
    }
    //根据日期（月份）筛选调薪记录
    @RequestMapping(value = "/salary/month/{date}")
    public RespPageEntity getAdjustSalarysByDate(
            @PathVariable(required = false) Date date,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size ) {
        return personnelService.getAdjustSalarysByDate(date, page, size);
    }

    @RequestMapping(value = "/salary",method = RequestMethod.POST)
    public RespBean addAdjustSalary(AdjustSalary adjustSalary){
        Employee employee = empService.getEmpById(adjustSalary.getEid());
        if(empService.getEmpById(adjustSalary.getEid())==null){
            return RespBean.error("员工号不存在");
        }
        if (!employee.getName().equals(adjustSalary.getEmpName())) {
            return RespBean.error("员工号与员工姓名不匹配！");
        }
        if(personnelService.addAdjustSalary(adjustSalary)==1){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }
    //更新更改员工实际薪资（调薪）
    @RequestMapping(value = "/salary/add",method = RequestMethod.PUT)
    public RespBean updateAdjustSalaryToAllSalary(AllSalary allSalary){
        Employee employee = empService.getEmpById(allSalary.getEid());
        if(empService.getEmpById(allSalary.getEid())==null){
            return RespBean.error("员工号不存在");
        }
        if (!employee.getName().equals(allSalary.getEmpName())) {
            return RespBean.error("员工号与员工姓名不匹配！");
        }
        if(personnelService.updateAdjustSalaryToAllSalary(allSalary)==1){
            return RespBean.ok("调薪成功");
        }
        return RespBean.error("调薪失败");
    }
    //添加更改员工实际薪资（录入账套薪资）
    @RequestMapping(value = "/salary/addnew",method = RequestMethod.PUT)
    public RespBean addAdjustSalaryToAllSalary(AllSalary allSalary){
        Employee employee = empService.getEmpById(allSalary.getEid());
        if(empService.getEmpById(allSalary.getEid())==null){
            return RespBean.error("员工号不存在");
        }
        if (!employee.getName().equals(allSalary.getEmpName())) {
            return RespBean.error("员工号与员工姓名不匹配！");
        }
        Integer result = personnelService.addAdjustSalaryToAllSalary(allSalary);
        if(result == 1 || result == 2){
            return RespBean.ok("录入成功");
        }
        return RespBean.error("录入失败");
    }

    @RequestMapping(value = "/salary",method = RequestMethod.GET)
    public List<AdjustSalary> getAllAdjustSalary(){
        return personnelService.getAllAdjustSalary();
    }

    @RequestMapping(value = "/salary",method = RequestMethod.PUT)
    public RespBean updateAdjustSalary(AdjustSalary adjustSalary){
        Employee employee = empService.getEmpById(adjustSalary.getEid());
        if(empService.getEmpById(adjustSalary.getEid())==null){
            return RespBean.error("员工号不存在");
        }
        if (!employee.getName().equals(adjustSalary.getEmpName())) {
            return RespBean.error("员工号与员工姓名不匹配！");
        }
        if(personnelService.updateAdjustSalary(adjustSalary)==1){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @RequestMapping(value = "/salary/{ids}",method = RequestMethod.DELETE)
    public RespBean deleteAdjustSalary(@PathVariable String ids){
        if(personnelService.deleteAdjustSalary(ids)==1){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    @GetMapping("/getmonthsalary")
    public RespBean getMonthSalary(@RequestParam Long eid,@RequestParam String empName) {
        Employee employee = empService.getEmpById(eid);
        if(empService.getEmpById(eid)==null){
            return RespBean.error("员工号不存在");
        }
        if (!employee.getName().equals(empName)) {
            return RespBean.error("员工号与员工姓名不匹配！");
        }
        AllSalary allSalary = salaryService.getSalaryByEid(employee.getId());
        return RespBean.ok(allSalary.getMonthSalary());
    }
}
